import Vue from 'vue'
import soocStorage from '../util/localStorage'

let baseUrl = 'http://172.16.14.113/dzjg/index.php';

export const actions = {
  //与后端接口进行交互
  doLogin(store) {
    let user = soocStorage.wrapJsonStorage('token');
    user.set('oauth_token', 'bfe4d8f8e7308aec2d8128c2d3579c17').set('oauth_token_secret', '6934fb5b6fc777820b123162f6351c69').save();
  },
  //获取首页课程列表
  getCourseList(store) {
    Vue.axios.post(baseUrl+'/apiv2/Course/allNewCourse').then((response) => {
      store.commit('get_course_list', response.data.data);
      store.commit('set_message', '');
    });
  },
  //得到首页banner
  getBanner(store) {
    Vue.axios.post(baseUrl+'/apiv2/Sooc/index_tj').then((response) => {
      store.commit('get_banners', response.data.data)
    });
  },
  //得到课程详细信息
  getCourseInfo(store, kcid) {
    Vue.axios.post(baseUrl+'/apiv2/Course/info', {kcid: kcid}).then((response) => {
      store.commit('get_course_info', response.data.data)
    });
  },
  //得到课程目录
  getCourseCatalog(store, kcid) {
    Vue.axios.post(baseUrl+'/apiv2/Course/newCatalog', {kcid: kcid}).then((response) => {
      store.commit('get_course_catalog', response.data.data)
    });
  },
  //得到课程分类
  getCourseClassify(store) {
    Vue.axios.post(baseUrl+'/apiv2/Course/departMenu').then((response) => {
      store.commit('get_course_classify', response.data)
    });
  },
  //查找课程操作
  searchCourses(store, params) {
    Vue.axios.post(baseUrl+'/apiv2/Course/allNewCourse', params).then((response) => {
      store.commit('search_courses', response.data.data);
      store.commit('set_keyword', params.keyword);
      if (response.data.data.length == 0) {
        store.commit('set_message', '没有找到你想要的课程');
      }
    });
  },
  //按分类进行课程查看
  ClassifyCourses(store,params){
    Vue.axios.post(baseUrl+'/apiv2/Course/allNewCourse', params).then((response) => {
      store.commit('classify_course', response.data.data);
    });
  },
  //设置显示消息
  setMessage(store, message) {
    store.commit('set_message', message);
  },
  //加入课程的操作
  addCourse(store, course) {
    course.select = 1;
    store.commit('add_course', course);
    // let params = store.state.user;
    // params.kcid = kcid;
    // Vue.axios.post(baseUrl+'/apiv2/Course/info',params).then((response) => {
    //   console.log(response.data);
    //   var result = response.data.data;
    //   result.isEncrypt = '1111';
    //   store.commit('get_course_info', response.data.data)
    // });
  }

}

export default actions